//
// Created by Hi on 25-7-22.
//

#ifndef RUNTIMEEXCEPTION_H
#define RUNTIMEEXCEPTION_H
#include <stdexcept>

/*************************************************
  * 描述：
  * 自定义异常类，继承自std::runtime_error
  * File：CustomException.h
  * Author：name@mozihealthcare.cn
  * Date：2025-07-18
  * Update：
  * ************************************************/
class CustomException :public std::runtime_error{

private:
    int errorCode; //异常码
public:
    enum EXCPTION_TYPE {
        FILE_ERROR=0,
        INVALID_INPUT,
        BUSSINESS_ERROR,
        CONCURRENT_ERROR,
        UNKNOWN_ERROR
    };
    CustomException(const std::string& message)
        :std::runtime_error(message),errorCode(BUSSINESS_ERROR) {}

    CustomException(const std::string& message, EXCPTION_TYPE type)
        :std::runtime_error(message),errorCode(type) {}

    const char* what() const noexcept override {
        static std::string fullMessage;
        fullMessage = "[" + getErrorTypeString() + "] " + std::runtime_error::what();
        return fullMessage.c_str();
    }

    // 获取错误类型的字符串表示
    std::string getErrorTypeString() const {
        switch ((errorCode)) {
            case FILE_ERROR: return "FILE_ERROR";
            case INVALID_INPUT: return "INVALID_INPUT";
            case BUSSINESS_ERROR: return "BUSINESS_ERROR";
            case CONCURRENT_ERROR: return "CONCURRENT_ERROR";
            case UNKNOWN_ERROR: return "UNKNOWN_ERROR";
            default: return "UNKNOWN_TYPE";
        }
    }

    // 获取错误码
    int getErrorCode() const noexcept {
        return errorCode;
    }
};

#endif //RUNTIMEEXCEPTION_H
